// 订单模块-有自己的state、reducer、type、actionCreator
const initState = {
  list: [
    {name: 'mac book pro', price: 14999, num: 1},
    {name: 'oppo FindX', price: 7999, num: 6}
  ]
}

const actionType = {
  DELORDER: 'DELORDER',
  ADDORDER: 'ADDORDER',
}

export const actionCreator = {
  delOrder: (index)=>({type: actionType.DELORDER, index: index}),
  addOrder: (info)=>({type: actionType.ADDORDER, info: info})
}

export const getList = (state)=> {
  return {...state.order}
}

const orderReducer = (state=initState, action)=>{
  switch (action.type) {
    case actionType.DELORDER:
      let arr = [...state.list];
      arr.splice(action.index, 1)
      return {
        // ...state,
        list: JSON.parse(JSON.stringify(arr))
      }
    case actionType.ADDORDER:
      // console.log(state, action);
      let arr1 = [...state.list];
      arr1.push(action.info)
      return {
        ...state,
        list: JSON.parse(JSON.stringify(arr1))
      }
  
    default:
      return {
        list: JSON.parse(JSON.stringify(state.list))
      }
  }
}

export default orderReducer